﻿using EAS.Content.Items;
using System.Collections.Generic;
using Terraria;
using Terraria.GameContent.Generation;
using Terraria.IO;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.WorldBuilding;

namespace EAS.Common.Systems
{
    public class AccSystem : ModSystem
    {
        public override void ModifyWorldGenTasks(List<GenPass> tasks, ref double totalWeight)
        {
            int num = tasks.FindIndex((GenPass genpass) => genpass.Name.Equals("Micro Biomes"));
            if (num != -1)
            {
                tasks.Insert(num + 1, new PassLegacy("Chest", new WorldGenLegacyMethod(GenerateExtraLoot)));
            }
            base.ModifyWorldGenTasks(tasks, ref totalWeight);
        }
        private void GenerateExtraLoot(GenerationProgress progress, GameConfiguration configuration)
        {
            progress.Message = Language.GetTextValue("Mods.EAS.Message.1") + Language.GetTextValue("Mods.EAS.Message.2");
            HashSet<ChestStyle> hashSet =
            [
                ChestStyle.Wooden,
                ChestStyle.Gold,
                ChestStyle.LockedGold,
                ChestStyle.RichMahogany,
                ChestStyle.Ivy,
                ChestStyle.LivingWood,
                ChestStyle.WebCovered,
                ChestStyle.Water,
                ChestStyle.Mushroom,
                ChestStyle.Granite,
                ChestStyle.Marble,
                ChestStyle.Lihzahrd,
                ChestStyle.LockedShadow
            ];
            Dictionary<ChestStyle, List<Chest>> dictionary = [];
            for (int i = 0; i < 1000; i++)
            {
                Chest chest = Main.chest[i];
                if (chest != null && WorldGen.InWorld(chest.x, chest.y, 42))
                {
                    Tile tile = Main.tile[chest.x, chest.y];
                    if (tile.TileType == 21 && chest.item != null)
                    {
                        ChestStyle chestStyle = (ChestStyle)(tile.TileFrameX / 36);
                        if (hashSet.Contains(chestStyle) &&
                            (chestStyle != ChestStyle.LockedGold || Main.wallDungeon[tile.WallType]) &&
                            (chestStyle != ChestStyle.Wooden || !Main.wallDungeon[tile.WallType]))
                        {
                            if (chestStyle == ChestStyle.Mushroom || chestStyle == ChestStyle.Granite || chestStyle == ChestStyle.Marble)
                            {
                                chestStyle = ChestStyle.Gold;
                            }
                            if (!dictionary.TryGetValue(chestStyle, out List<Chest> value))
                            {
                                value = [];
                                dictionary[chestStyle] = value;
                            }

                            value.Add(chest);
                        }
                    }
                }
            }
            if (dictionary.ContainsKey(ChestStyle.Skyware))
            {
                ReplaceItemInChests(dictionary[ChestStyle.Skyware], ModContent.ItemType<GravityHorseshoe>(), 0.75f);
            }
        }
        private static void ReplaceItemInChests(IList<Chest> chestList, int newItem, float chance = 0.1f, int Stack = 1)
        {
            System.ArgumentNullException.ThrowIfNull(chestList);
            for (int i = 0; i < chestList.Count; i++)
            {
                Chest chest = chestList[i];
                Item item = chest.item[0];
                if (item != null && !item.IsAir && Utils.NextFloat(Main.rand) <= chance)
                {
                    item.SetDefaults(newItem);
                    item.stack = Stack;
                }
            }
        }
        private static void AddItemsInChests(IList<Chest> chestList, int newItem, int Stack = 1)
        {
            for (int i = 0; i < chestList.Count; i++)
            {
                Chest chest = chestList[i];
                for (int j = 0; j < chest.item.Length; j++)
                {
                    Item item = chest.item[j];
                    if (chest.item[j - 1] != null && !chest.item[j - 1].IsAir && (item == null || item.IsAir))
                    {
                        item.SetDefaults(newItem);
                        item.stack = Stack;
                    }
                }
            }
        }
        private static void TypeItemsInChests(IList<Chest> chestList, int ReplaceItem, int newItem, float chance = 1f, int Stack = 1)
        {
            for (int i = 0; i < chestList.Count; i++)
            {
                Chest chest = chestList[i];
                for (int j = 0; j < chest.item.Length; j++)
                {
                    Item item = chest.item[j];
                    if (item != null && !item.IsAir && item.type == ReplaceItem && Utils.NextFloat(Main.rand) <= chance)
                    {
                        item.SetDefaults(newItem);
                        item.stack = Stack;
                    }
                }
            }
        }
        public enum ChestStyle : byte
        {
            ///木箱
            Wooden,
            ///金箱
            Gold,
            ///上锁金箱
            LockedGold,
            ///暗影箱
            Shadow,
            ///上锁暗影箱
            LockedShadow,
            ///木桶
            Barrel,
            ///垃圾桶
            TrashCan,
            ///乌木箱
            Ebonwood,
            ///红木箱
            RichMahogany,
            ///珍珠木箱
            Pearlwood,
            ///常青藤箱
            Ivy,
            ///冰雪箱
            Ice,
            ///生命木箱
            LivingWood,
            ///天空箱
            Skyware,
            ///暗影木箱
            Shadewood,
            ///蜘蛛箱
            WebCovered,
            ///神庙箱
            Lihzahrd,
            ///水下箱
            Water,
            ///丛林箱
            Jungle,
            ///腐化箱
            Corruption,
            ///血腥箱
            Crimson,
            ///神圣箱
            Hallowed,
            ///冰冻箱
            Frozen,
            ///上锁丛林箱
            LockedJungle,
            ///上锁腐化箱
            LockedCorruption,
            ///上锁血腥箱
            LockedCrimson,
            ///上锁神圣箱
            LockedHallowed,
            ///上锁冰冻箱
            LockedFrozen,
            ///王朝木箱
            Dynasty,
            ///蜜蜂箱
            Honey,
            ///齿轮箱
            Steampunk,
            ///棕榈木箱
            PalmWood,
            ///夜光蘑菇箱
            Mushroom,
            ///北地木箱
            BorealWood,
            ///史莱姆箱
            Slime,
            ///绿地牢箱
            GreenDungeon,
            ///上锁绿地牢箱
            LockedGreenDungeon,
            ///粉地牢箱
            PinkDungeon,
            ///上锁粉地牢箱
            LockedPinkDungeon,
            ///蓝地牢箱
            BlueDungeon,
            ///上锁蓝地牢箱
            LockedBlueDungeon,
            ///骨箱
            Bone,
            ///仙人掌箱
            Cactus,
            ///血肉箱
            Flesh,
            ///地狱箱
            Obsidian,
            ///南瓜箱
            Pumpkin,
            ///阴森木箱
            Spooky,
            ///玻璃箱
            Glass,
            ///火星人箱
            Martian,
            ///陨星箱
            Meteorite,
            ///花岗岩箱
            Granite,
            ///大理石箱
            Marble,
            ///水晶箱
            Crystal,
            ///24K纯金箱
            Golden
        }
    }
}
